package com.yun.zf.admin.util;

import lombok.extern.slf4j.Slf4j;
import org.reflections.ReflectionUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Set;

@Slf4j
@Component
public class ReflectionObjectUtil {

    public BigDecimal checkIsNull(Object obj, String path){
        Class c =  ReflectionUtils.forName(path);
        Set<Field> fields =   ReflectionUtils.getAllFields(c);
        int i=0;
        for (Field f : fields) {
            f.setAccessible(true);
            try {
                Object oj =  f.get(obj);
                if (oj!=null){
                   i++;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
      int total =  fields.size();
        DecimalFormat df=new DecimalFormat("0.0");
      String st =  df.format(((float)i/total)*100);

      return new BigDecimal(st) ;
    }
}
